from errors import Missing

_parkings: list[dict] = []


def create_parking_json(
    address: str, opened: bool, count_places: int, count_available_places: int
) -> dict:
    parking = {
        "id": len(_parkings) + 200,
        "address": address,
        "opened": opened,
        "count_places": count_places,
        "count_available_places": count_available_places,
    }
    _parkings.append(parking)
    return parking


def get_parking_json(parking_id: int) -> dict:
    for parking in _parkings:
        if parking["id"] == parking_id:
            return parking

    raise Missing(msg=f"Parking with id={parking_id} does not exist")


def update_parking_json(
    parking_id: int,
    address: str,
    opened: bool,
    count_places: int,
    count_available_places: int,
) -> dict:
    for parking in _parkings:
        if parking["id"] == parking_id:
            parking["address"] = address
            parking["opened"] = opened
            parking["count_places"] = count_places
            parking["count_available_places"] = count_available_places
            return parking

    raise Missing(msg=f"Parking with id={parking_id} does not exist")
